﻿/*
=======================================================
    Message【消息】
=======================================================
*/

namespace Services
{
    public interface IMessageLogic : ILogic<MessageModel, MessageFilterModel>
    {
        bool CanJump(int id);
    }


    public class MessageLogic : BaseLogic<MessageModel, MessageFilterModel, Message, IMessageRepository, IMessageMapping, IMessageService>, IMessageLogic
    {
        private readonly IPermissionRepository _IPermissionRepository;

        public MessageLogic(ICurrentUser currentUser, IMessageRepository repository, IMessageMapping mapping, IMessageService service, IPermissionRepository iPermissionRepository)
            : base(currentUser, repository, mapping, service)
        {
            _IPermissionRepository = iPermissionRepository;
        }

        #region 判断消息是否可以转向
        public bool CanJump(int id)
        {
            var entity = _repository.GetByID(id);

            //修改消息已读
            entity.Status = (int)Message_Status.已读;
            _repository.Update(entity);

            if (entity.Type == (int)Message_Type.备绳维保)
            {
                if (_IPermissionRepository.CurrentUserHasPermission(Permission_PerID.备绳维保_查询))
                {
                    return true;
                }
            }
            else if (entity.Type == (int)Message_Type.在用绳检测)
            {
                if (_IPermissionRepository.CurrentUserHasPermission(Permission_PerID.在用绳检测_查询))
                {
                    return true;
                }
            }

            throw new CustomException("无权查看该模块的数据，请联系管理员！");
        }
        #endregion
    }
}
